package com.nenude.corejava.ch13;

import java.util.Random;

/**
 * <pre>
 * 线程开发步骤2：
 * 1·编写一个类，实现java.lang.Runnable 接口
 * 2·实现线程的run方法
 * 3·运行一个线程：
 * 
 * 
 * </pre>
 * 
 * @author admin
 *
 */
public class ThreadDemo2 implements Runnable {

	private int count;

	@Override
	public void run() {
		while (count <= 10) {
			try {
				Thread.sleep(new Random().nextInt(2000));
				System.out.println(count++);

			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {

		ThreadDemo2 demo2 = new ThreadDemo2();
		// 错误写法：注意：实现Runnable接口没有start方法
		// demo2.start();

		Thread thread = new Thread(demo2);// 需要把Runnable接口实现类包装成Thread类的对象，才能执行start方法
		thread.start();

	}

}
